package pe.gob.onpe.facade;

import java.util.List;
import java.util.logging.Level;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import pe.gob.onpe.ejb.LogUtil;
import pe.gob.onpe.ejb.PadronElectoralOnpe;
import pe.gob.onpe.facade.local.PadronElectoralOnpeFacadeLocal;
import pe.gob.onpe.facade.remote.PadronElectoralOnpeFacadeRemote;
import pe.gob.onpe.util.Persistencia;


/**
 * Facade for entity PadronElectoralOnpe.
 * 
 * @see pe.gob.onpe.ejb.PadronElectoralOnpe
 * @author MyEclipse Persistence Tools
 */
@Stateless
public class PadronElectoralOnpeFacade implements
		PadronElectoralOnpeFacadeLocal, PadronElectoralOnpeFacadeRemote {
	// property constants
	public static final String DIGITOVERIFICACION = "digitoverificacion";
	public static final String NUMEROMESA = "numeromesa";
	public static final String UBIGEO_1 = "ubigeo_1";
	public static final String APELLIDOPATERNO = "apellidopaterno";
	public static final String APELLIDOMATERNO = "apellidomaterno";
	public static final String NOMBRES = "nombres";
	public static final String SEXO = "sexo";
	public static final String GRADOINSTRUCCION = "gradoinstruccion";
	public static final String TIPODOCUMENTO = "tipodocumento";

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Perform an initial save of a previously unsaved PadronElectoralOnpe
	 * entity. All subsequent persist actions of this entity should use the
	 * #update() method.
	 * 
	 * @param entity
	 *            PadronElectoralOnpe entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(PadronElectoralOnpe entity) {
		LogUtil.log("saving PadronElectoralOnpe instance", Level.INFO, null);
		try {
			entityManager.persist(entity);
			LogUtil.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent PadronElectoralOnpe entity.
	 * 
	 * @param entity
	 *            PadronElectoralOnpe entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(PadronElectoralOnpe entity) {
		LogUtil.log("deleting PadronElectoralOnpe instance", Level.INFO, null);
		try {
			entity = entityManager.getReference(PadronElectoralOnpe.class,
					entity.getNumerodocumento());
			entityManager.remove(entity);
			LogUtil.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved PadronElectoralOnpe entity and return it or a
	 * copy of it to the sender. A copy of the PadronElectoralOnpe entity
	 * parameter is returned when the JPA persistence mechanism has not
	 * previously been tracking the updated entity.
	 * 
	 * @param entity
	 *            PadronElectoralOnpe entity to update
	 * @returns PadronElectoralOnpe the persisted PadronElectoralOnpe entity
	 *          instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public PadronElectoralOnpe update(PadronElectoralOnpe entity) {
		LogUtil.log("updating PadronElectoralOnpe instance", Level.INFO, null);
		try {
			PadronElectoralOnpe result = entityManager.merge(entity);
			LogUtil.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			LogUtil.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public PadronElectoralOnpe findById(String id) {
		LogUtil.log("finding PadronElectoralOnpe instance with id: " + id,
				Level.INFO, null);
		try {
			PadronElectoralOnpe instance = entityManager.find(
					PadronElectoralOnpe.class, id);
			return instance;
		} catch (RuntimeException re) {
			LogUtil.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all PadronElectoralOnpe entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the PadronElectoralOnpe property to query
	 * @param value
	 *            the property value to match
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
	 *            row index in the query result-set to begin collecting the
	 *            results. rowStartIdxAndCount[1] specifies the the maximum
	 *            number of results to return.
	 * @return List<PadronElectoralOnpe> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<PadronElectoralOnpe> findByProperty(String propertyName,
			final Object value, final int... rowStartIdxAndCount) {
		LogUtil.log("finding PadronElectoralOnpe instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from PadronElectoralOnpe model where model."
					+ propertyName + "= :propertyValue";
			Query query = entityManager.createQuery(queryString);
			query.setParameter("propertyValue", value);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find by property name failed", Level.SEVERE, re);
			throw re;
		}
	}

	public List<PadronElectoralOnpe> findByDigitoverificacion(
			Object digitoverificacion, int... rowStartIdxAndCount) {
		return findByProperty(DIGITOVERIFICACION, digitoverificacion,
				rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByNumeromesa(Object numeromesa,
			int... rowStartIdxAndCount) {
		return findByProperty(NUMEROMESA, numeromesa, rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByUbigeo_1(Object ubigeo_1,
			int... rowStartIdxAndCount) {
		return findByProperty(UBIGEO_1, ubigeo_1, rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByApellidopaterno(
			Object apellidopaterno, int... rowStartIdxAndCount) {
		return findByProperty(APELLIDOPATERNO, apellidopaterno,
				rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByApellidomaterno(
			Object apellidomaterno, int... rowStartIdxAndCount) {
		return findByProperty(APELLIDOMATERNO, apellidomaterno,
				rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByNombres(Object nombres,
			int... rowStartIdxAndCount) {
		return findByProperty(NOMBRES, nombres, rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findBySexo(Object sexo,
			int... rowStartIdxAndCount) {
		return findByProperty(SEXO, sexo, rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByGradoinstruccion(
			Object gradoinstruccion, int... rowStartIdxAndCount) {
		return findByProperty(GRADOINSTRUCCION, gradoinstruccion,
				rowStartIdxAndCount);
	}

	public List<PadronElectoralOnpe> findByTipodocumento(Object tipodocumento,
			int... rowStartIdxAndCount) {
		return findByProperty(TIPODOCUMENTO, tipodocumento, rowStartIdxAndCount);
	}

	/**
	 * Find all PadronElectoralOnpe entities.
	 * 
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
	 *            row index in the query result-set to begin collecting the
	 *            results. rowStartIdxAndCount[1] specifies the the maximum
	 *            count of results to return.
	 * @return List<PadronElectoralOnpe> all PadronElectoralOnpe entities
	 */
	@SuppressWarnings("unchecked")
	public List<PadronElectoralOnpe> findAll(final int... rowStartIdxAndCount) {
		LogUtil.log("finding all PadronElectoralOnpe instances", Level.INFO,
				null);
		try {
			final String queryString = "select model from PadronElectoralOnpe model";
			Query query = entityManager.createQuery(queryString);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Long obtenerCantidadRegistros() {
		
		String consulta = "SELECT count(p) FROM PadronElectoralOnpe p";
		Query query = entityManager.createQuery(consulta);
		Long resultado = Persistencia.getSingleResult(query,Long.class);
		return resultado;

	}
}